<template>
  <el-dialog
    v-model="show"
    :title="mode === 'add' ? '添加用户' : '编辑用户'"
    @close="handleClose"
    width="600px"
    class="modern-dialog"
  >
    <el-form
      :model="formData"
      label-width="100px"
      ref="formRef"
      class="modern-form"
    >
      <el-form-item label="用户昵称" prop="nickname">
        <el-input
          v-model="formData.nickname"
          placeholder="请输入用户昵称"
          class="modern-input"
        />
      </el-form-item>

      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="formData.mobile"
          placeholder="请输入手机号码"
          class="modern-input"
        />
      </el-form-item>
      <el-form-item label="用户密码" prop="password" v-if="mode === 'add'">
        <el-input
          v-model="formData.password"
          type="password"
          placeholder="请输入用户密码"
          class="modern-input"
          show-password
        />
      </el-form-item>

      <el-form-item label="用户密码" prop="password" v-else>
        <el-input
          v-model="formData.password"
          type="password"
          placeholder="不输入则不修改密码"
          class="modern-input"
          show-password
        />
        <div class="form-tip">不输入密码则表示不修改密码</div>
      </el-form-item>

      <el-form-item label="用户头像" prop="avatar">
        <image-upload
          v-model:modelValue="formData.avatar"
          :limit="1"
          :multiple="false"
          class="modern-upload"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose" class="modern-button-cancel">
          取消
        </el-button>
        <el-button
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          class="modern-button-submit"
        >
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage, type FormInstance } from 'element-plus'
import imageUpload from '@/components/image-upload/index.vue'
import { updateUserApi } from '@/api/user'

const show = ref(false)
const loading = ref(false)
const emit = defineEmits(['success'])
const formRef = ref<FormInstance>()
const mode = ref<'add' | 'edit'>('add')

const formData = ref({
  id: 0,
  nickname: '',
  mobile: '',
  email: '',
  password: '',
  avatar: [],
  status: 1,
})

const defaultForm = () => ({
  id: 0,
  nickname: '',
  mobile: '',
  email: '',
  password: '',
  avatar: [],
  status: 1,
})




const handleClose = () => {
  show.value = false
  loading.value = false
  formRef.value?.clearValidate()
}

const handleSubmit = async () => {
  if (!formRef.value) return

  const valid = await formRef.value.validate().catch(() => false)
  if (!valid) return

  loading.value = true
  try {
    const res: any = await updateUserApi(formData.value)
    ElMessage.success(res.message || (mode.value === 'add' ? '添加成功' : '更新成功'))
    handleClose()
    emit('success')
  } finally {
    loading.value = false
  }
}

const getDetails = (data: any) => {
  formData.value = { ...defaultForm(), ...data }
  formData.value.password = '' // 清空密码
}

defineExpose({
  open(type: 'add' | 'edit') {
    show.value = true
    mode.value = type
  },
  setFormData(data: any) {
    getDetails(data)
  },
})
</script>

<style scoped>
.modern-dialog {
  border-radius: 12px;
}

.modern-form {
  padding: 8px 0;
}

.modern-input {
  border-radius: 6px;
}

.modern-upload {
  width: 100%;
}

.modern-radio {
  display: flex;
  gap: 16px;
}

.form-tip {
  font-size: 12px;
  color: #6b7280;
  margin-top: 4px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding-top: 12px;
  border-top: 1px solid #e5e7eb;
}

.modern-button-cancel {
  border-radius: 6px;
  border: 1px solid #d1d5db;
  color: #374151;
  background: white;
}

.modern-button-cancel:hover {
  border-color: #9ca3af;
  color: #111827;
}

.modern-button-submit {
  border-radius: 6px;
  background: #3b82f6;
  border: 1px solid #3b82f6;
}

.modern-button-submit:hover {
  background: #2563eb;
  border-color: #2563eb;
}

:deep(.el-dialog__header) {
  margin: 0;
  padding: 16px 20px;
  border-bottom: 1px solid #e5e7eb;
}

:deep(.el-dialog__title) {
  font-size: 16px;
  font-weight: 600;
  color: #111827;
}

:deep(.el-dialog__body) {
  padding: 20px;
}

:deep(.el-dialog__footer) {
  padding: 12px 20px 16px;
  margin: 0;
}

:deep(.el-form-item__label) {
  color: #374151;
  font-weight: 500;
}

:deep(.el-radio__label) {
  color: #374151;
}

@media (max-width: 768px) {
  .modern-dialog {
    width: 90% !important;
    margin: 0 auto;
  }

  .dialog-footer {
    flex-direction: column;
    gap: 8px;
  }

  .modern-button-cancel,
  .modern-button-submit {
    width: 100%;
  }
}
</style>
